package com.oraclejava.phonehistory;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract.Data;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ContentProvider_phoneHistoryActivity extends Activity {
	private static String[] mProjections = new String[] { Data._ID,
			CallLog.Calls.DATE, CallLog.Calls.NUMBER };
	ListView listView1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initialize();

		Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
				mProjections, null, null, CallLog.Calls.DATE + " Desc");
		startManagingCursor(cursor);

//		Long time = Long.valueOf(CallLog.Calls.DATE);

		ListAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_2, cursor, 
				new String[] {timeToString(cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE))), CallLog.Calls.NUMBER },
				new int[] {android.R.id.text1, android.R.id.text2 });
		listView1.setAdapter(adapter);
	}

	private String timeToString(Long time) {
		SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String date = simpleFormat.format(new Date(time));
		return date;
	}

	private void initialize() {
		setContentView(R.layout.main);
		listView1 = (ListView) findViewById(R.id.listView1);

	}
}